#!/usr/bin/ruby

##################################
#        MFroute.rb                                                                           
#        description;                                                                          
#            provide route control functions for MFd.rb                                        
#        version           1.7                                                             
#       Last Change, updated error reporting
##################################

RT_VERSION=1.7
def route_ver
   return "\tROUTE library version: #{RT_VERSION}\n"
end

def mroute(line,type)
    retary=Array.new
    tmp=line.gsub('-mhost','')
    t2=tmp.split(' ')
    if !t2[0].index('-')
        return "\tFAILED:  adding mhost #{line}\n"
    end
    addp=t2.shift.split('-')
    a2=addp[0].split('.')
    first=a2[3].to_i
    last=addp[1].to_i
    base="#{a2[0]}.#{a2[1]}.#{a2[2]}."
    while first <= last
        cline="-host #{base}#{first} #{t2.join(' ')}"
        if (type == "del")
            if !system("#{$route} del #{cline}")
                retary << "\tFAILED: deleting route #{cline}(mhost: #{line})\n"
            end
        else
            if !system("#{$route} add #{cline}")
                retary << "\tFAILED: adding route #{cline} (mhost: #{line})\n"
            end        
        end
        first +=1
    end
    return retary.to_s
end

def modrt_zone(zone,type)
    retary=Array.new
    if !test(?r, "/jail/acl/zones/#{zone}/interface")  
        return "\tFAILED: zone #{zone} is invalid\n"
    elsif !test(?r,"/jail/acl/zones/#{zone}/interface/int")
        return "\tFAILED: zone #{zone} interface is invalid\n"    
    else
        if active?("/jail/acl/zones/#{zone}")
            int=MFreadlines("/jail/acl/zones/#{zone}/interface/int").shift
            lines=MFreadlines("/jail/routes/rules")
            lines.each {|line|
                next if !line.index(/#{int}$/) && !line.index(/#{int} /)
                if type == "del"
                    if line.index("-mhost")
                        retary << mroute(line,"del")
                    elsif !system("#{$route} del #{line}")
                        retary << "\tFAILED: deleting route #{line}\n"
                    end
                else
                    if line.index("-mhost")
                        retary << mroute(line,"add")                        
										elsif ipv6(line)
											if !system("#{$iproute} add #{line}")
					            	retary << "\tFAILED: adding route #{line} line94 \n"
											end
                    elsif !system("#{$route} add #{line}")
                        retary << "\tFAILED: adding route #{line}\n"
                    end
                end
                }
            return retary.to_s
        else
            return "\tNOTICE: zone #{zone} is deactivated by config.. skipping\n"
        end
    end
end

def modrt_all(type)
    retary=Array.new
    lines=MFreadlines("/jail/routes/rules")
    lines.each {|line|
    if type == "del"
        if line.index("-mhost")
            retary << mroute(line,"del")
        elsif $ipv4int
					if !system("#{$route} del #{line}")
            retary << "\tFAILED: deleting route #{line}\n"
					end
        end
    else
        if line.index("-mhost")
            retary << mroute(line,"add")
				elsif ipv6(line)
					if !system("#{$iproute} add #{line}")
            retary << "\tFAILED: adding route #{line} line94 \n"
					end

        elsif $ipv4int
					if !system("#{$route} add #{line}")
            retary << "\tFAILED: adding route #{line}\n"
					end
        end
    end
    }
return retary.to_s
end


def route_stop(zone="all")
    if active?("/jail/routes")
        if zone == "all"
            return modrt_all("del")
        else
            return modrt_zone(zone,"del")
        end
    else
        return "\tNOTICE: ROUTES not active in config... skipping\n"
    end
end

def route_start(zone="all")
    if active?("/jail/routes")
        if zone == "all"
            return modrt_all("add")
        else
            return modrt_zone(zone,"add")
        end
    else
        return "\tNOTICE: ROUTES not active in config... skipping\n"
    end
end

def route_restart(zone)
    route_stop(zone)
    route_start(zone)
end
